package com.openlogic.common.logic.log.strategy;

import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.AppBeanLoadEndEvent;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.util.MultiMap;

import com.alibaba.fastjson2.JSON;
import com.openlogic.api.logic.entity.RuleExecLog;
import com.openlogic.common.logic.annotation.RuleLog;
import com.openlogic.common.logic.constants.RuleTypeConstants;
import com.openlogic.common.logic.log.factory.RecordLogFactory;

import cn.hutool.core.util.ObjectUtil;

/**
 * @ClassName: DecisionTreeLogService
 * @Description: 决策表日志处理实现
 * @author weiyongpeng
 * @date 2024年10月9日 上午9:22:24
 */
@Component
public class DecisionTableLogService extends RecordDecisionLogStrategy {

	@Override
	protected void getControllerMethodDescription(Context joinPoint, RuleLog ruleLog, RuleExecLog operateLog,
			Object jsonResult) throws Exception {
		// 获取参数的信息，传入到数据库中。
		super.setRequestValue(joinPoint, operateLog);
		// 出参
//    	net.sf.json.JSONObject fromObject = net.sf.json.JSONObject.fromObject(net.sf.json.JSONObject.fromObject(jsonResult).get(AjaxResult.RESULT_TAG));
//    	if (Optional.ofNullable(fromObject.get("execLog")).isPresent()) {
//    		String rep = fromObject.get("execLog").toString();
//    		operateLog.setRepParam(rep);
//		}
//    	if(Optional.ofNullable(fromObject.get("execStatus")).isPresent() && !Boolean.valueOf(fromObject.get("execStatus").toString())) {
//    		if (Optional.ofNullable(fromObject.get("result")).isPresent()) {
//        		String rep = fromObject.get("result").toString();
//        		operateLog.setRepParam(rep);
//    		}
//    		operateLog.setStatus(ExecConstants.ERROR);
//    		operateLog.setErrorMsg(operateLog.getRepParam());
//    	}
	}

	/**
	 * 描述：参数拼装
	 * @Title: argsArrayToString
	 * @param paramsArray
	 * @return
	 * @author weiyongpeng
	 * @date 2024年5月7日 上午10:21:14
	 */
	@Override
	protected String argsArrayToString(MultiMap<String> paramMap, RuleExecLog operLog) {
		String params = "";
		if (paramMap != null) {
			paramMap.forEach(x -> {
				Object[] values = x.getValues().toArray();
				for (int i = 0; i < values.length; i++) {
					Object o = values[i];
					if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 0) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 入参
							operLog.setReqParam(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 1) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 决策树json字符串
							operLog.setRuleInfo(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 2) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 规则链id
							operLog.setRuleId(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 3) {
						try {
							Object jsonObj = JSON.toJSON(o);
							operLog.setRuleName(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 4) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 规则链原始json
							operLog.setRuleRawValue(jsonObj.toString());
						} catch (Exception e) {
						}
					}
				}
			});
		}
		return params.trim();
	}

	@Override
	public void onEvent(AppBeanLoadEndEvent event) throws Throwable {
		RecordLogFactory.register(RuleTypeConstants.decisionTable.getCode(), this);
	}

}
